/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/

#include "pch.h"
#include <db/db_table_instance.h>
#include <assert.h>

namespace test_wrappers { namespace storage {

table_instance::table_instance(): 
    _tableId(-1)
{
}

table_instance::table_instance(
    const table_definition& tableDefinition,
    const table_id& tableId,
    const std::shared_ptr<tuple_instance>& tupleInstance) :
    _tableId(tableId),
    _tupleInstance(tupleInstance),
    _name(tableDefinition.name())
{
    std::vector<index_definition> indices = tableDefinition.indices();
    for (auto &index : indices)
    {
        std::shared_ptr<index_instance> indexInstance =
            index_instance::create(index, tupleInstance);
        _indices[indexInstance->userIndexId()] = indexInstance;
    }
}

const table_id& table_instance::id() const
{
    return _tableId;
}

const std::wstring& table_instance::name() const
{
    return _name;
}

const std::shared_ptr<tuple_instance>& table_instance::tupleInstance() const
{
    return _tupleInstance;
}

std::shared_ptr<index_instance> table_instance::index(
    const object_id& indexId
    ) const
{
    if ( _indices.find(indexId) == _indices.end() )
    {
        throw db_exception("unknown index", e_db_unknown_index);
    }
    return _indices.at(indexId);
}

tuple_values table_instance::createValues() const
{
    std::shared_ptr<std::vector<std::shared_ptr<field_value>>> fieldValues =
        std::make_shared<std::vector<std::shared_ptr<field_value>>>();

    const std::vector<std::shared_ptr<field_instance>>& fieldInstances = _tupleInstance->fieldInstances();

    for (auto &fieldInstance : fieldInstances)
    {
        std::shared_ptr<field_value> fieldValue;

        switch (fieldInstance->type())
        {
        case type_unknown:
            break;
        case type_int16:
            fieldValue = createValue<unsigned short>(fieldInstance->fieldInstanceId(), 0);
            break;
        case type_int32:
            fieldValue = createValue<unsigned int>(fieldInstance->fieldInstanceId(), 0);
            break;
        case type_int64:
            fieldValue = createValue<unsigned __int64>(fieldInstance->fieldInstanceId(), 0);
            break;
        case type_float32:
            fieldValue = createValue<float>(fieldInstance->fieldInstanceId(), 0.0f);
            break;
        //case type_string:
        //    fieldValue = (std::dynamic_pointer_cast<field_definition_t<std::wstring>>(field))->createValue(L"");
        //    break;
        case type_string_long:
            fieldValue = createValue<std::wstring>(fieldInstance->fieldInstanceId(), L"");
            break;
        case type_binaryBlob:
            fieldValue = createValue<binary_blob>(fieldInstance->fieldInstanceId(), 0);
            break;
        default:
            _ASSERTE(false);
        }
        fieldValues->push_back(fieldValue);
    }

    return tuple_values(_tupleInstance, fieldValues);
}

}} // test_wrappers::storage
